import {ApiResult} from "@/phr/common/ApiResult";
import {BmiData} from "@/phr/service/bmi/BmiData";
import HttpClient from '@/phr/common/HttpClient';


class BmiService{

    private GET_BMI_URL = "bmi/get";
    private ADD_WEIGHT_URL = "bmi/weight/add";
    private UPDATE_HEIGHT_URL = "bmi/height/update";

    /**
     * 获取当前登录用户的BMI数据
     * 返回的数组是以时间降序
     * @returns {Promise<ApiResult<BmiData>>}
     */
    public async getBmiData():Promise<ApiResult<BmiData>>{
        return HttpClient.get(this.GET_BMI_URL);
    }

    /**
     * 添加当前用户的体重信息
     * @param {number} weight 体重，单位为公斤，如50.3
     * @param {string} time 如果不传该参数，那么后台将时间设为当前时间
     * 预留这个参数是为了更好的测试，要符合yyyy-mm-ddThh-mm-ss格式
     * @returns {Promise<void>} 200表示添加正确
     */
    public async addWeight(weight:number,time:string = null):Promise<ApiResult>{
        let params = {
            weight:weight
        };
        if (time !=null){
            params['datetime'] = time;
        }
        return HttpClient.post(this.ADD_WEIGHT_URL,params);
    }

    /**
     * 更新当前用户的身高
     * @param {number} height 身高，单位为cm，如173
     * @returns {Promise<ApiResult>}
     */
    public async updateHeight(height:number):Promise<ApiResult>{
        let params = {
            height:height
        };
        return HttpClient.post(this.UPDATE_HEIGHT_URL,params);
    }

}

export default new BmiService();